/*
 * BenchmarkDailyDumpCheckJob
 * Created: Dec 19, 2006 
 * by abargnesi<mailto:abargnesi@aquent.com/>
 */
package com.den4.jynform.tasks;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.den4.jynform.alerts.EmailLogger;

public class FileCheckJob implements Job {
    private EmailLogger emailLogger;
    private String taskMsg;
    private String taskSubject;
    private Log log = LogFactory.getLog(FileCheckJob.class); 
    
    public void execute(JobExecutionContext ctx) throws JobExecutionException {
        Map jobMap = ctx.getJobDetail().getJobDataMap();
        emailLogger = (EmailLogger) jobMap.get("emailLogger");

        FilenameGenerator fGenerator = (FilenameGenerator) jobMap.get("generator");
        String yesterday = fGenerator.generate();
        taskSubject = "BenchmarkDailyDumpCheck - " + yesterday;
        
        //Read out dump location.
        String filePath = (String) jobMap.get("filePath");
        if(filePath == null || "".equals(filePath)) {
            taskMsg = "The 'Path' is not correctly set.";
            log.info(taskSubject);
            log.info(taskMsg);
            emailLogger.log(taskSubject, taskMsg);
        }

        //Check for yesterday's daily dump csv file in the dump location.
        File yesterdayFile = new File(filePath.concat(yesterday).concat(".csv"));
        boolean fileExists = yesterdayFile.exists();
        if(!fileExists) {
            taskMsg = "The file ".concat(yesterdayFile.getAbsolutePath()).concat(" doesn't exist.\n");
            taskMsg = taskMsg.concat("The data for ").concat(yesterday).concat(" is not recorded.  Fix!");
            log.info(taskSubject);
            log.info(taskMsg);
            emailLogger.log(taskSubject, taskMsg);
        } else {
            taskMsg = "The file ".concat(yesterdayFile.getAbsolutePath()).concat(" exists.");
            log.info(taskSubject);
            log.info(taskMsg);
            emailLogger.log(taskSubject, taskMsg);
            
            int numBytes = 0;
            try {
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(yesterdayFile));
                while(bis.read() != -1) {
                    numBytes++;
                }
            } catch(Exception e) {
                taskMsg = "The data for ".concat(yesterday).concat(" could not be sized up.");
                log.info(taskSubject);
                log.info(taskMsg);
                emailLogger.log(taskSubject, taskMsg);
            }
            
            //RULE OF THUMB - If file is bigger than 10kb then we have some data.
            if(numBytes >= 10000) {
                taskMsg = "There is quality data for ".concat(yesterday).concat(".  Ok to import!");
                log.info(taskSubject);
                log.info(taskMsg);
                emailLogger.log(taskSubject, taskMsg);
            } else {
                taskMsg = "There is insufficient data for ".concat(yesterday).concat(".  Import is questionable, needs examination.");
                log.info(taskSubject);
                log.info(taskMsg);
                emailLogger.log(taskSubject, taskMsg);
            }
        }
    }
}
